package dao

import (
	"github.com/duke-git/lancet/v2/slice"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

// 角色与菜单对应
type SysRoleMenuDao struct {
	DB           *gorm.DB
	UserRolesDao *SysUserRolesDao
	Log          *zap.Logger
}

type RolePermsBO struct {
	RoleCode string   //角色编码
	Perms    []string //权限标识集合
}

type RolePerms struct {
	RoleCode string //角色编码
	Perm     string //权限标识
}

// 获取权限和拥有权限的角色列表
func (sysRoleMenuDao *SysRoleMenuDao) GetRolePermsList(roleCode string) []RolePermsBO {
	sql := `SELECT
            t3.perm,
            t2.code role_code
        FROM
            sys_role_menu t1
            INNER JOIN sys_role t2 ON t1.role_id = t2.id AND t2.is_deleted = 0 AND t2.status = 1
            INNER JOIN sys_menu t3 ON t1.menu_id = t3.id
            where type = 4` //type = 4 是按钮
	tx := sysRoleMenuDao.DB.Raw(sql)
	if roleCode != "" {
		tx.Where(" t2.`code` = ?", roleCode)
	}
	var roles []RolePerms
	var m = make(map[string][]string)
	tx.Find(&roles)

	for _, rolePerm := range roles {
		code := rolePerm.RoleCode
		perm := rolePerm.Perm
		perms := m[code]
		if perms == nil || len(perms) == 0 {
			m[code] = []string{perm}
		} else {
			if !slice.Contain(perms, perm) {
				perms = append(perms, perm)
			}
			m[code] = perms
		}
	}
	var bos []RolePermsBO
	for k, v := range m {
		var bo RolePermsBO
		bo.RoleCode = k
		bo.Perms = v
		bos = append(bos, bo)
	}
	return bos
}
